package com.atkusen.mini.minidanke.service.impl;

import com.atkusen.mini.common.entity.MiniUserEntity;
import com.atkusen.mini.common.entity.User;
import com.atkusen.mini.minidanke.dao.MiniUserDao;
import com.atkusen.mini.minidanke.service.MiniUserService;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atkusen.mini.common.utils.PageUtils;
import com.atkusen.mini.common.utils.Query;


@Service("miniUserService")
public class MiniUserServiceImpl extends ServiceImpl<MiniUserDao, MiniUserEntity> implements MiniUserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MiniUserEntity> page = this.page(
                new Query<MiniUserEntity>().getPage(params),
                new QueryWrapper<MiniUserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public User findByUsername(String username) {
        User user = new User();
        String[] usernameAndPass = username.split("_");
        QueryWrapper<MiniUserEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_name",usernameAndPass[0]).eq("user_phone",usernameAndPass[1]);
        MiniUserEntity miniUserEntity = baseMapper.selectOne(queryWrapper);
        if(miniUserEntity!=null){
            user.setUserName(miniUserEntity.getUserName());
            String password = new BCryptPasswordEncoder().encode(miniUserEntity.getUserPhone());
            user.setPassword(password);
            user.setUserId(miniUserEntity.getUserId());
        }
        return user;
    }

    @Override
    public Set<String> findPermissions(String username) {
        Set<String> permissions = new HashSet<>();
        permissions.add("sys:user:view");
        permissions.add("sys:user:add");
        permissions.add("sys:user:edit");
        permissions.add("sys:user:delete");
        permissions.add("sys:user:get");
        return permissions;
    }

}